package td;

import ui.DwarfView;

public class Dwarf extends Enemy {
	public Dwarf(Field position, DoTListener listener) {
		super(position, listener);
		this.hp = 20;
		this.speed = 3;
		this.value = 3;
		this.damage = 3;
		this.viewable = new DwarfView(this);
	}

	public void step() {
		/* Asks the field to get one of its steppable neighbours */
		Field newField = position.getSteppable();
		/* Adds the dwarf to that next field, and removes from current */
		newField.addElement(this);
		position.removeElement(this);
		position = newField;

		/* If new field has a trap, applies it's effect */
		if (position.hasTrap) {
			applyTrapEffect(position.getTrap());
		}

		/* If the new field is the endpoint, the player loses hp */
		if (position.isEndPoint) {
			listener.enemyWon(this);
			position.removeElement(this);
		}
	}

	/* Applies the effect of the trap the dwarf has stepped into. */
	public void applyTrapEffect(Trap trap) {
		if (trap instanceof FireTrap)
			applyTrapEffect((FireTrap) trap);
		else if (trap instanceof WindTrap)
			applyTrapEffect((WindTrap) trap);
		else if (trap instanceof VineTrap)
			applyTrapEffect((VineTrap) trap);

		this.position.removeElement(trap);
		this.position.hasTrap = false;
	}

	/*
	 * If it's a Oneshotter FireTrap it instantly kills the dwarf, otherwise
	 * deals damage and sets on fire
	 */
	public void applyTrapEffect(FireTrap ftrap) {
		if (ftrap.dmgType.equals(DamageTypes.OneShot)) {
			this.die();
		} else if (onFire == false) {
			fireduration = 2;
			onFire = true;
			fireDamage = ftrap.damage;
			listener.enemyHasDoT(this);
		}
		else{
			fireduration = 2;
		}

		listener.steppedInFireTrap(ftrap);
	}

	/*
	 * If it's a Windtrap, it knocks the dwarf back to previous fields, and if
	 * it has rainbow upgrade, it also slows it down
	 */
	public void applyTrapEffect(WindTrap wtrap) {
		this.position.removeElement(wtrap);
		this.position.hasTrap = false;
		position.removeElement(this);
		position = position.getPushbackField(wtrap.pushback);
		position.addElement(this);
		if (wtrap.status.equals(StatusEffects.Slow)) {
			speed += 1;
			slowduration = 2;
			slowed = true;
			listener.enemyHasDoT(this);
		}

		listener.steppedInWindTrap(wtrap);
	}

	/*
	 * If it's a VineTrap it slows down the dwarf, and if it has rainbow
	 * upgrade, it also deals damage
	 */
	public void applyTrapEffect(VineTrap vtrap) {
		speed += vtrap.slowLevel;
		slowduration = 2;
		slowlvl = vtrap.slowLevel;
		slowed = true;
		listener.enemyHasDoT(this);
		if (vtrap.doesDmg) {
			hp -= 5;
		}
		listener.steppedInVineTrap(vtrap);
	}

	/*
	 * Method to create two small enemies when a CutterBullet hits and splits it
	 * in half.
	 */
	public void cutInHalf() {
		Dwarf smallDwarf1 = new Dwarf(position, listener);
		Dwarf smallDwarf2 = new Dwarf(position, listener);
		smallDwarf1.hp = this.hp / 2;
		smallDwarf2.hp = this.hp / 2;
		smallDwarf1.viewable.update();
		smallDwarf2.viewable.update();
		position.removeElement(this);
		position.addElement(smallDwarf1);
		position.addElement(smallDwarf2);
		listener.cutInHalf(this, smallDwarf1, smallDwarf2);
	}
}
